﻿using System;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
using Omniscient.Foundation.ApplicationModel;
using Omniscient.TimeTracker.BusinessModel;
using Omniscient.Foundation.Data;

namespace Omniscient.TimeTracker.TaskBarMenu
{
    public class NotifierTray
    {
        private NotifierContextMenu _menu;
        private NotifyIcon _notifyIcon;
        private ITimeEntryManager _manager;
        private Window _parentForm;

        public NotifierTray(Window parentForm, TaskBarMenuModel model)
        {
            _menu = new NotifierContextMenu(model);
            _notifyIcon = new NotifyIcon();

            _parentForm = parentForm;

            _manager = ApplicationManager.Current.ObjectContainer.Get<ITimeEntryManager>();
            _manager.StateChanged += new StateChangedEventHandler(_manager_StateChanged);

            _notifyIcon.MouseDoubleClick += new MouseEventHandler(_notifyIcon_MouseDoubleClick);
            _notifyIcon.MouseUp += new MouseEventHandler(_notifyIcon_MouseUp);

            _notifyIcon.Icon = new Icon(this.GetType().Assembly.GetManifestResourceStream("Omniscient.TimeTracker.TaskBarMenu.Resources.IconStop.ico"));

            _notifyIcon.Visible = true;
        }


        private void _manager_StateChanged(object sender, EventArgs e)
        {
            if (_manager.HasRunningTask)
                _notifyIcon.Icon = new Icon(this.GetType().Assembly.GetManifestResourceStream("Omniscient.TimeTracker.TaskBarMenu.Resources.IconStart.ico"));
            else
                _notifyIcon.Icon = new Icon(this.GetType().Assembly.GetManifestResourceStream("Omniscient.TimeTracker.TaskBarMenu.Resources.IconStop.ico"));

            ShowTrayBallonTip();
        }


        private void _notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (_parentForm.WindowState == WindowState.Normal || _parentForm.WindowState == WindowState.Maximized)
                _parentForm.WindowState = WindowState.Minimized;
            else
                _parentForm.WindowState = WindowState.Normal;

            _parentForm.Topmost = true;
        }

        private void _notifyIcon_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                _menu.StaysOpen = false;
                _menu.Placement = System.Windows.Controls.Primitives.PlacementMode.Mouse;
                _menu.IsOpen = true;
            }
            else if (e.Button == MouseButtons.Left)
            {
                ShowTrayBallonTip();
            }
        }

        private void ShowTrayBallonTip()
        {
            if (_manager.HasRunningTask)
            {
                _notifyIcon.BalloonTipTitle = _manager.CurrentRunningTimeEntry.Task.ParentProject.Name;
                if (_manager.CurrentRunningTimeEntry.Category == null || 
                    _manager.CurrentRunningTimeEntry.Category.Status == EntityStatus.NotLoadedYet)
                    _notifyIcon.BalloonTipText = String.Format("Runing task : {0} Category : {1}", _manager.CurrentRunningTimeEntry.Task.Name, "None");
                else
                    _notifyIcon.BalloonTipText = String.Format("Runing task : {0} Category : {1}", _manager.CurrentRunningTimeEntry.Task.Name, _manager.CurrentRunningTimeEntry.Category.Name);
            }
            else
            {
                _notifyIcon.BalloonTipText = "No current running task.";
            }

            _notifyIcon.ShowBalloonTip(2000);
        }
    }
}
